const express = require('express');
const cookieParser = require('cookie-parser')
const app = express();

// 配置中间件
app.use(cookieParser('123456'))

app.get('/', (req, res) => {
  // 设置cookie 如果cookie没有过期的话，关闭浏览器后重新打开，cookie不会销毁
  // res.cookie("name",'zhangsan', {maxAge: 1000*60*60, path: '/article'}); 
  // res.cookie("name",'zhangsan', {maxAge: 1000*60*60, httpOnly: true}); 
  
  // 多个域名共享cookie aaa.itying.com bbb.itying,com 二级域名不同
  // C:\Windows\System32\drivers\etc\hosts 更改域名 端口设置80
  // res.cookie("name",'zhangsan', {maxAge: 1000*60*60, domain: ".itying.com"}); 
  
  // 中文cookie
  // res.cookie("name",'张三', {maxAge: 1000*60*60}); 

  // cookie的加密
  res.cookie("name",'张三', {maxAge: 1000*60*60, signed: true});
  res.send('首页')
})

app.get('/article', (req, res) => {
  res.send('新闻列表:' + req.signedCookies.name)
})

app.get('/user', (req, res) => {

  // Cookies that have not been signed
  console.log('Cookies: ', req.cookies)
  
  // Cookies that have been signed
  console.log('Signed Cookies: ', req.signedCookies)

  res.send('用户：' + req.cookies.name)
})

app.listen(3003)